package com.autumn.demo.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.autumn.common.api.application.dto.input.DefaultPrimaryKeyInput;
import com.autumn.common.api.application.dto.input.PageQueryInput;
import com.autumn.common.api.application.dto.output.IPageQueryResult;
import com.autumn.demo.dto.input.NameQueryInput;
import com.autumn.demo.dto.output.BasicsInfoOutput;
import com.autumn.demo.services.IDemoDataBaseService;
import com.autumn.web.ContextServiceRequestHeader;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author 老码农
 *
 *         2017-12-01 17:35:45
 */
@RestController
@RequestMapping("/demo")
@Api(tags = "动态数据源")
@ContextServiceRequestHeader
// @PreAuthorize(DemoController.AUTHORIZE_HASROLE_MEMBER)
public class DemoController {

	/**
	 * 注意：所有 ulr 必须是小写，这是区分大小写的
	 * 
	 */

	@Autowired
	private IDemoDataBaseService service;

	@Autowired
	private WebApplicationContext applicationContext;

	@GetMapping("/getAllUrl")
	@ResponseBody
	public List<String> getAllUrl() {
		RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
		// 获取url与类和方法的对应信息
		Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();
		List<String> urlList = new ArrayList<>();
		for (RequestMappingInfo info : map.keySet()) {
			// 获取url的Set集合，一个方法可能对应多个url
			Set<String> patterns = info.getPatternsCondition().getPatterns();
			for (String url : patterns) {
				urlList.add(url);
			}
		}
		return urlList;
	}

	@Autowired
	@Value("${server.port}")
	private String serverPort;

	/**
	 * 获取职务信息
	 * 
	 * @param input
	 *            输入
	 * @return
	 * @author 杨昌国 2017-12-01 17:08:41
	 */
	@ApiOperation("获取职务信息")
	@GetMapping("/dutiesinfo")
	public BasicsInfoOutput getDutiesInfo(@RequestBody DefaultPrimaryKeyInput input) {
		return service.getDutiesInfo(input);
	}

	@GetMapping("/dutieslist")
	@ApiOperation("职务列表")
	public List<BasicsInfoOutput> dutiesList() {
		return service.dutiesList();
	}

	@GetMapping("/nationlist")
	@ApiOperation("民族列表")
	public List<BasicsInfoOutput> nationList() {
		return service.nationList();
	}

	@GetMapping("/nationinfo")
	@ApiOperation("根据名称获取民族信息")
	public BasicsInfoOutput getNationInfo(@RequestBody @Valid NameQueryInput input) {
		return service.getNationInfo(input);
	}

	@ApiOperation("民族分页")
	@RequestMapping(path = "/nationPage", method = { RequestMethod.GET, RequestMethod.POST })
	public IPageQueryResult<BasicsInfoOutput> nationPage(@RequestBody @Valid PageQueryInput input) {
		return service.nationPage(input);
	}

	@ApiOperation("添加职务")
	@RequestMapping(path = "/addduties", method = { RequestMethod.POST })
	public BasicsInfoOutput addDuties() {
		return service.addDuties();
	}

	@ApiOperation("添加民族")
	@RequestMapping(path = "/addnation", method = { RequestMethod.POST })
	public BasicsInfoOutput addNation() {
		return service.addNation();
	}

	@ApiOperation("添加职务和民族")
	@RequestMapping(path = "/adddutiesornation", method = { RequestMethod.POST })
	public BasicsInfoOutput addDutiesOrNation() {
		return service.addDutiesOrNation();
	}
}
